
#include <QDateTime>

#include "CRemoteControlData.h"

//-------------------------------------------------------------------------------------------------

qint32 CFileTransferData::m_iNextTransferID = 1;

CFileTransferData::CFileTransferData(QTcpSocket* pSocket, const QString& sSourceName, const QString& sTargetName, qint32 iFileSize, bool bOut)
: m_pSocket(pSocket)
, m_pFile(NULL)
, m_tLastIncomingMessageTime(QDateTime::currentDateTime())
, m_sSourceName(sSourceName)
, m_sTargetName(sTargetName)
, m_iFileSize(iFileSize)
, m_iOffsetInFile(0)
, m_bOut(bOut)
, m_bDone(false)
, m_bCompCRC(false)
, m_bSameCRC(false)
, m_bFileInfoSent(false)
, m_bGotFileInfo(false)
, m_bEchoFileCRC(false)
{
	m_ulTransferID = getNewTransferID();
}

CFileTransferData::CFileTransferData(QTcpSocket* pSocket, const QString& sSourceName, const QString& sTargetName, qint32 iFileSize, qint32 iTransferID, bool bOut)
: m_pSocket(pSocket)
, m_pFile(NULL)
, m_tLastIncomingMessageTime(QDateTime::currentDateTime())
, m_sSourceName(sSourceName)
, m_sTargetName(sTargetName)
, m_iFileSize(iFileSize)
, m_iOffsetInFile(0)
, m_bOut(bOut)
, m_bDone(false)
, m_bCompCRC(false)
, m_bSameCRC(false)
, m_bFileInfoSent(false)
, m_bGotFileInfo(false)
, m_bEchoFileCRC(false)
{
	m_ulTransferID = iTransferID;
	m_iNextTransferID = iTransferID + 1;
}

CFileTransferData::~CFileTransferData()
{
	if (m_pFile != NULL)
	{
		if (m_pFile->isOpen()) m_pFile->close();

		delete m_pFile;
	}
}

// Generates a file transfer ID using current time and an auto-incremented integer
qint32 CFileTransferData::getNewTransferID()
{
	m_iNextTransferID++;

	return QDateTime::currentDateTime().time().second() + QDateTime::currentDateTime().time().msec() + m_iNextTransferID;
}
